<template>
  <a-select
    :placeholder="placeholder"
    v-model="select_codeId"
    style="width:100%"
    :allowClear="allowClear"
  >
    <a-select-option
      :value="item.value"
      v-for="item in dictionaryData"
      :key="item.value"
    >{{item.label}}</a-select-option>
  </a-select>
</template>

<script>
import { listDictionary } from "@/api/common";
export default {
  name: "DictionarySelect",
  model: {
    prop: "codeId",
    event: "change"
  },
  props: {
    //数据字典标识
    codeItemId: {
      required: true,
      type: String
    },
    //字典id
    codeId: {
      type: String
    },
    size: {
      type: String,
      default: "small"
    },
    placeholder: {
      type: String,
      default: "请选择"
    },
    allowClear: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dictionaryData: {}, //数据字典数据
      select_codeId: this.codeId
    };
  },
  created() {
    this.getDictionaryData();
  },
  methods: {
    getDictionaryData() {
      listDictionary(this.codeItemId).then(response => {
        this.dictionaryData = response.data.data;
      });
    }
  },
  watch: {
    select_codeId(val) {
      this.$emit("change", val);
    },
    codeId: function(val) {
      this.select_codeId = val;
    }
  }
};
</script>